
private String updateExistingRecord(JSONObject record, String jdyRecordId) throws Exception {
    OkHttpClient client = new OkHttpClient();
    MediaType mediaType = MediaType.parse("application/json");
    JSONObject requestBody = new JSONObject();
    requestBody.put("app_id", jdyAppId);
    requestBody.put("entry_id", jdyEntryId);
    requestBody.put("data_id", jdyRecordId); // 添加 data_id 字段

    JSONObject dataItem = new JSONObject();
    JSONObject values = record.getJSONObject("values");

    // 判空处理并提供默认值
    JSONArray policyNumberArray = values.getJSONArray("♻️保单号3️⃣4️⃣");
    dataItem.put("_widget_1744296547868",
        new JSONObject().put("value", policyNumberArray != null && !policyNumberArray.isEmpty() ? 
            policyNumberArray.getJSONObject(0).getString("text") : ""));

    dataItem.put("_widget_1744296547849",
        new JSONObject().put("value", 
            values.getJSONArray("♻️受保人中文名1️⃣") != null && 
            !values.getJSONArray("♻️受保人中文名1️⃣").isEmpty() ? 
            values.getJSONArray("♻️受保人中文名1️⃣").getJSONObject(0).getString("text") : ""));

    dataItem.put("_widget_1744296547850",
        new JSONObject().put("value", 
            values.getJSONArray("♻️受保人英文名1️⃣2️⃣") != null && 
            !values.getJSONArray("♻️受保人英文名1️⃣2️⃣").isEmpty() ? 
            values.getJSONArray("♻️受保人英文名1️⃣2️⃣").getJSONObject(0).getString("text") : ""));

    Double firstYearPremium = values.getDoubleValue("♻️首年应缴保费(含征费)3️⃣4️⃣");
    dataItem.put("_widget_1744296547873",
        new JSONObject().put("value", firstYearPremium != null ? firstYearPremium : 0.0));

    JSONArray currencyArray = values.getJSONArray("♻️保单货币1️⃣");
    dataItem.put("_widget_1744296547854",
        new JSONObject().put("value", currencyArray != null && !currencyArray.isEmpty() ? 
            currencyArray.getJSONObject(0).getString("text") : ""));

    JSONArray regionArray = values.getJSONArray("♻️保单地区1️⃣");
    dataItem.put("_widget_1744296547843",
        new JSONObject().put("value", regionArray != null && !regionArray.isEmpty() ? 
            regionArray.getJSONObject(0).getString("text") : ""));

    requestBody.put("data", dataItem);

    RequestBody body = RequestBody.create(mediaType, requestBody.toJSONString());
    Request request = new Request.Builder()
        .url("https://api.jiandaoyun.com/api/v5/app/entry/data/update")
        .post(body)
        .addHeader("Authorization", "Bearer " + jdyApiKey)
        .addHeader("Content-Type", "application/json")
        .build();

    try (Response response = client.newCall(request).execute()) {
        if (response.code() == 200) {
            return "同步成功";
        } else {
            return "同步失败";
        }
    }
}

    // 获取普通字段值的方法
    private String getFieldValue(JSONObject values, String fieldName) {
        try {
            JSONArray fieldArray = values.getJSONArray(fieldName);
            return fieldArray != null && !fieldArray.isEmpty() ?
                fieldArray.getJSONObject(0).getString("text") : "";
        } catch (Exception e) {
            return "";
        }
    }

    // 新增：获取URL类型字段值的方法（支持多个URL）
    private String getUrlsFieldValue(JSONObject values, String fieldName) {
        try {
            JSONArray fieldArray = values.getJSONArray(fieldName);
            if (fieldArray == null || fieldArray.isEmpty()) {
                return "";
            }

            StringBuilder urls = new StringBuilder();
            for (int i = 0; i < fieldArray.size(); i++) {
                JSONObject fieldObj = fieldArray.getJSONObject(i);
                if (fieldObj.containsKey("file_url")) {
                    String url = fieldObj.getString("file_url");
                    if (urls.length() > 0) {
                        urls.append(",");
                    }
                    urls.append(url);
                }
            }
            return urls.toString();
        } catch (Exception e) {
            return "";
        }
    }
